package codeRandomThoughts.Test46全排列;

import java.util.ArrayList;
import java.util.List;

public class Solution2 {
    private List<Integer> path = new ArrayList<>();
    private List<List<Integer>> result = new ArrayList<>();

    boolean[] used = new boolean[21];

    public List<List<Integer>> permute(int[] nums) {
        backtracking(nums);
        return result;
    }

    public void backtracking(int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
        }

        for (int i = 0; i < nums.length; i++) {
            if (!used[nums[i]+10]) {
                used[nums[i]+10] = true;
                path.add(nums[i]);
                backtracking(nums);
                path.remove(path.size() - 1);
                used[nums[i]+10] = false;
            }
        }
    }
}
